package com.yao.mybatis.config;


import com.yao.mybatis.sqlsource.SqlSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dom4j.Element;

/**
 * 将每个语句解析成statement
 *
 * @author Administrator
 */
public class XMLStatementParser {
    private static final Logger LOGGER = LogManager.getLogger("mylog");
    private Configuration configuration;

    public XMLStatementParser(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * 解析每个sql语句
     *
     * @param element
     * @param namespace
     * @return
     */
    public void parse(Element element, String namespace) {
        MappedStatement mappedStatement = new MappedStatement();
        mappedStatement.setId(namespace + "." + element.attributeValue("id"));
        mappedStatement.setParameterType(element.attributeValue("parameterType"));
        mappedStatement.setResultType(element.attributeValue("resultType"));
        //开始解析sql语句
        mappedStatement.setSqlSource(creatSql(element));
        configuration.addStatement(namespace + "." + element.attributeValue("id"), mappedStatement);
    }

    private SqlSource creatSql(Element element) {
        XMLScriptParser xmlScriptParser = new XMLScriptParser();
        return xmlScriptParser.parse(element);
    }
}
